//
//  STMarketingVC.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/10.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STMarketingVC.h"
#import "STReduceCell.h"
#import "STVolumeCell.h"
#import "UIButton+ZYHblock.h"
#import "STAddMarketVC.h"
#import "SWCouponModel.h"

@interface STMarketingVC ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView * volumeListView;
    NSMutableArray * volumeArr;
}
@property(nonatomic, strong) NSArray<SWCouponModel*>*dataArray;
@end

@implementation STMarketingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"优惠卷";
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(actionAddSelfVolumes)];
    self.navigationItem.rightBarButtonItem =rightBarBtn;
    
    [self loadVolumeData];
    [self loadVolumeListView];
    
}

#pragma mark - 网络请求
//加载数据
-(void)loadVolumeData{
    
    BasicRequest * request = [[BasicRequest alloc] initWithParams:@{@"stylistId":[YLUserInfo sharedInstance].userID}];
    request.URL = [NSString stringWithFormat:@"%@stylistCoupon/getAll",BASEURL];
    request.requestMethod = HttpRequestMethodRawPOST;
    [request startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        
        NSLog(@"%@",responseObject);
        NSNumber *code = responseObject[@"code"];
        if (code.integerValue == 200) {
            NSArray *array = responseObject[@"data"];
            if (array.count > 0) {
                _dataArray = [NSArray modelArrayWithClass:[SWCouponModel class] json:array];
            }
            [volumeListView reloadData];
        }
        
    } Failure:nil];
}

#pragma mark - 初始化UI
-(void)loadVolumeListView{
    volumeListView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    volumeListView.delegate = self;
    volumeListView.dataSource = self;
    volumeListView.separatorStyle = UITableViewCellSelectionStyleNone;
    volumeListView.backgroundColor = [UIColor clearColor];
    volumeListView.tableFooterView = [UIView new];
    volumeListView.sectionHeaderHeight = 10;
    [self.view addSubview:volumeListView];
    
    [volumeListView registerNib:[UINib nibWithNibName:[STReduceCell className] bundle:nil] forCellReuseIdentifier:[STReduceCell className]];
    [volumeListView registerNib:[UINib nibWithNibName:[STVolumeCell className] bundle:nil] forCellReuseIdentifier:[STVolumeCell className]];
}

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  146;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //满减卷
    WeakSelf
    SWCouponModel *model = _dataArray[indexPath.row];
    if (model.type == 1) {
        
        STVolumeCell * cell = [tableView dequeueReusableCellWithIdentifier:[STVolumeCell className]];
        cell.model = model;
        cell.clickBlockAction = ^(SWCouponModel *model, UIButton *btn) {
            [weakSelf dowmCoupon:model btn:btn];
        };
        return cell;
    }
    
    STReduceCell * cell = [tableView dequeueReusableCellWithIdentifier:[STReduceCell className]];
    cell.model = model;
    cell.clickBlockAction = ^(SWCouponModel *model, UIButton *btn) {
        [weakSelf dowmCoupon:model btn:btn];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - 事件
-(void)dowmCoupon:(SWCouponModel *)model btn:(UIButton *)btn{
    
    if (model.status == 0) {
        [self downAction:model status:@"1" content:@"您确定上架？" btn:btn];
    } else if(model.status == 1){
        [self downAction:model status:@"0" content:@"是否确认下架该优惠券，下架后用户无法继续领取该优惠券，已领取按设定使用" btn:btn];
    }
}

- (void)downAction:(SWCouponModel *)model  status:(NSString *)status content:(NSString *)content btn:(UIButton *)btn{
 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:content preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *action1= [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        BasicRequest * request = [[BasicRequest alloc] initWithParams:@{@"stylistCouponId":model.id, @"status":status}];
        request.URL = [NSString stringWithFormat:@"%@stylistCoupon/editPutaway",BASEURL];
        request.requestMethod = HttpRequestMethodRawPOST;
        [request startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
            
            NSNumber *code = responseObject[@"code"];
            if (code.integerValue == 200) {
                [YLProgressHUDManager showSuccess:responseObject[@"message"]];
                model.status = status.integerValue;
                if (status.integerValue ==1) {
                    [btn setTitle:@"下架" forState:UIControlStateNormal];
                }else {
                   [btn setTitle:@"上架" forState:UIControlStateNormal];
                }
                
            }
            
        } Failure:nil];
    }];
    [alertController addAction:action];
    [alertController addAction:action1];
    [self presentViewController:alertController animated:YES completion:nil];
    
}





-(void)actionAddSelfVolumes{
    WeakSelf
    STAddMarketVC *vc =  [STAddMarketVC new];
    vc.uploadSuccessBlock = ^{
        [weakSelf loadVolumeData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
@end
